<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>sampler 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-bucket-sampler-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-sampler-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-sampler-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-bucket-sampler-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-bucket-sampler-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-bucket.html">桶(bucket)聚合</a></span>
»
<span class="breadcrumb-node">sampler 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-bucket-reverse-nested-aggregation.html">« reverse_nested 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-significantterms-aggregation.html">significant_terms 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-bucket-sampler-aggregation"></a>采样器(sampler)聚合
</h2>
</div></div></div>
<p>一种过滤聚合，用于将任何子聚合的处理限制在得分最高的文档样本中。</p>
<div class="ulist itemizedlist">
<p class="title"><strong>使用用例举例：</strong></p>
<ul class="itemizedlist">
<li class="listitem">
将分析的焦点集中在高相关性匹配上，而不是低质量匹配的潜在长尾上
</li>
<li class="listitem">
降低聚合的运行成本，仅使用样本即可产生有用的结果，如<code class="literal">significant_terms</code>
</li>
</ul>
</div>
<p>示例：</p>
<p>
在StackOverflow数据中查询流行的词项<code class="literal">javascript</code>或较罕见的词项<code class="literal">kibana</code>，将会匹配许多文档——其中大多数都缺少单词Kibana。

为了将<code class="literal">significant_terms</code>聚合集中在最有可能匹配查询中最感兴趣部分的得分最高的文档上，我们使用了一个示例。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /stackoverflow/_search?size=0
{
    "query": {
        "query_string": {
            "query": "tags:kibana OR tags:javascript"
        }
    },
    "aggs": {
        "sample": {
            "sampler": {
                "shard_size": 200
            },
            "aggs": {
                "keywords": {
                    "significant_terms": {
                        "field": "tags",
                        "exclude": ["kibana", "javascript"]
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/509.console"></div>
<p>响应：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "sample": {
            "doc_count": 200,<a id="CO240-1"></a><i class="conum" data-value="1"></i>
            "keywords": {
                "doc_count": 200,
                "bg_count": 650,
                "buckets": [
                    {
                        "key": "elasticsearch",
                        "doc_count": 150,
                        "score": 1.078125,
                        "bg_count": 200
                    },
                    {
                        "key": "logstash",
                        "doc_count": 50,
                        "score": 0.5625,
                        "bg_count": 50
                    }
                ]
            }
        }
    }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO240-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>
总共采样了200份文件。因此，执行嵌套的<code class="literal">significant_terms</code>聚合的成本是有限的，而不是无限的。
</p>
</td>
</tr>
</table>
</div>
<p>
如果没有<code class="literal">sampler</code>聚合，请求查询会考虑低质量匹配的完整“长尾”，因此会识别不太重要的词项，如<code class="literal">jquery</code>和<code class="literal">angular</code>，而不是关注更有见地的与Kibana相关的词项。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /stackoverflow/_search?size=0
{
    "query": {
        "query_string": {
            "query": "tags:kibana OR tags:javascript"
        }
    },
    "aggs": {
             "low_quality_keywords": {
                "significant_terms": {
                    "field": "tags",
                    "size": 3,
                    "exclude":["kibana", "javascript"]
                }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/510.console"></div>
<p>响应：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "low_quality_keywords": {
            "doc_count": 600,
            "bg_count": 650,
            "buckets": [
                {
                    "key": "angular",
                    "doc_count": 200,
                    "score": 0.02777,
                    "bg_count": 200
                },
                {
                    "key": "jquery",
                    "doc_count": 200,
                    "score": 0.02777,
                    "bg_count": 200
                },
                {
                    "key": "logstash",
                    "doc_count": 50,
                    "score": 0.0069,
                    "bg_count": 50
                }
            ]
        }
    }
}</pre>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_shard_size_2"></a>shard_size
</h3>
</div></div></div>
<p>
参数<code class="literal">shard_size</code>限制了在每个分片上处理的样本中收集多少得分最高的文档。默认值为<code class="literal">100</code>。
</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_limitations_4"></a>局限性
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="sampler-breadth-first-nested-agg"></a>不能嵌套在广度优先(<code class="literal">breadth_first</code>)聚合下
</h4>
</div></div></div>
<p>
作为基于质量的过滤器，<code class="literal">sampler</code>聚合需要访问为每个文档产生的相关性分数。

因此，它不能嵌套在将<code class="literal">collect_mode</code>从默认的深度优先(<code class="literal">depth_first</code>)模式切换到广度优先(<code class="literal">breadth_first</code>)模式的<code class="literal">terms</code>聚合下，因为这会丢弃分数。

在这种情况下，将会抛出一个错误。
</p>
</div>

</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-bucket-reverse-nested-aggregation.html">« reverse_nested 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-significantterms-aggregation.html">significant_terms 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>